//
//  MydataEditViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/17.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "MydataEditViewController.h"
#import "GFDatePicker.h"
#import "GFAddressPicker.h"
#import "GFSexView.h"
#import "BRTextField.h"
@interface MydataEditViewController ()<UITableViewDelegate,UITableViewDataSource,GFAddressPickerDelegate,GetBabySexInformation,UITextFieldDelegate>
/** 姓名 */
@property (nonatomic, strong) BRTextField *nameTF;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UITableView *tablview;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,copy)NSString *time;
@property(nonatomic,strong)GFAddressPicker *pickerView;
@property(nonatomic,strong)GFSexView *sexView;

@end

@implementation MydataEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNavagationBackBtn];
    self.title = @"资料编写";
    [self createTableViewUI];
    // Do any additional setup after loading the view.
}
-(void)createNavagationBackBtn{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 6, 67, 23);
    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigator_btn_back"]];
    backImg.frame = CGRectMake(-10, 0, 22, 22);
    [backBtn addSubview:backImg];
    UILabel *backText = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 65, 22)];
    backText.text = @"返回";
    backText.font = [UIFont systemFontOfSize:17];
    [backText setBackgroundColor:[UIColor clearColor]];
    [backText setTextColor:[UIColor whiteColor]];
    [backBtn addSubview:backText];
    [backBtn addTarget:self action:@selector(leftBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem setLeftBarButtonItem:leftButton];
}
-(void)leftBarButtonItemPressed:(UIButton *)btn{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 初始化tablview
 */
-(void)createTableViewUI{
    self.tablview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tablview];
    self.tablview.scrollEnabled = NO;
    self.tablview.delegate = self;
    self.tablview.dataSource = self;
    UIView *footerView = [[UIView alloc] init];
    self.tablview.tableFooterView = footerView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self addImageView:cell];
        
    }
    if (indexPath.row == 1) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self setupNameTF:cell];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
        {

        }
            break;
        case 1:
        {
            
        }
            break;
        case 2:
        {
            self.sexView = [[GFSexView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            self.sexView.delegate = self;
            [self.view addSubview:self.sexView];
        }
            break;
        case 3:
        {
            [GFDatePicker showCustomDatePickerAtView:self.view choosedDateBlock:^(NSDate *date) {
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                NSString *dateStr = [NSString stringWithFormat:@"%@",date];
                cell.detailTextLabel.text = [dateStr componentsSeparatedByString:@" "][0];
            } cancelBlock:^{
                
            }];
        }
            break;
        case 4:
        {
            self.pickerView = [[GFAddressPicker alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            [self.pickerView updateAddressAtProvince:@"河南省" city:@"郑州市" town:@"金水区"];
            self.pickerView.delegate = self;
            self.pickerView.font = [UIFont boldSystemFontOfSize:18];
            [self.view addSubview:self.pickerView];
        }
            break;
        case 5:
        {
            
        }
            break;
        default:
            break;
    }
}
#pragma mark -地址的代理方法
- (void)GFAddressPickerCancleAction
{
    [self.pickerView removeFromSuperview];
}

- (void)GFAddressPickerWithProvince:(NSString *)province
                               city:(NSString *)city area:(NSString *)area
{
    [self.pickerView removeFromSuperview];
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:4 inSection:0];
    UITableViewCell * cell = [self.tablview cellForRowAtIndexPath:indexpath];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@  %@  %@",province,city,area];
    
    NSLog(@"%@  %@  %@",province,city,area);
}
#pragma mark -性别的代理方法
-(void)getBabySexInformation:(NSString *)sex{
    [self.sexView removeFromSuperview];
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:2 inSection:0];
    UITableViewCell * cell = [self.tablview cellForRowAtIndexPath:indexpath];
    cell.detailTextLabel.text = sex;
}
-(void)GFBabySexPickerCancleAction{
    [self.sexView removeFromSuperview];
}
- (BRTextField *)getTextField:(UITableViewCell *)cell {
    BRTextField *textField = [[BRTextField alloc]initWithFrame:CGRectMake(WIDTH - 230, 0, 200, 50)];
    textField.backgroundColor = [UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:16.0f];
    textField.textAlignment = NSTextAlignmentRight;
    textField.delegate = self;
    [cell.contentView addSubview:textField];
    return textField;
}
-(void)addImageView:(UITableViewCell *)cell{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 80, 0, 50, 50)];
    self.imageView.backgroundColor = [UIColor cyanColor];
    [cell.contentView addSubview:self.imageView];
    
}
#pragma mark - 姓名 textField
- (void)setupNameTF:(UITableViewCell *)cell {
    if (!_nameTF) {
        _nameTF = [self getTextField:cell];
        _nameTF.placeholder = @"请输入";
        _nameTF.returnKeyType = UIReturnKeyDone;
        _nameTF.tag = 0;
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 0 || textField.tag == 4) {
        [textField resignFirstResponder];
    }
    return YES;
}
-(NSArray *)titleArray{
    if (_titleArray == nil) {
        _titleArray = @[@"头像",@"昵称",@"性别",@"生日",@"地区",@"个性签名"];
    }
    return _titleArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
